﻿@{
    ViewBag.Title = "ModelItem";
    Layout = "~/Views/Shared/_Main.cshtml";
}
<input id="hddId" type="hidden" value="@Model.GUID" />
<input id="hddDefaultView" type="hidden" value="@Model.DefaultView" />
<input id="hddSchemaName" type="hidden" value="@Model.SchemaName" />
<input id="hddTableDescription" type="hidden" value="@Model.TableDescription" />
<script type="text/javascript">
    $(function () {
        $('#dd').dialog({
            title: '搜索条件',
            buttons: [{
                text: '查询',
                iconCls: 'icon-ok',
                handler: function () {
                    SearchData();
                    $('#dd').dialog('close');
                }
            }, {
                text: '清除',
                handler: function () {
                    $("#whereConditionDataGrid").datagrid({
                        url: '/GenericSearch/GetModelColumns?dbName=' + encodeURI($.getUrlParam("dbName")) + '&id=' + $("#hddId").val()
                    });
                }
            }, {
                text: '关闭',
                handler: function () {
                    $('#whereConditionDataGrid').datagrid('acceptChanges');
                    $('#dd').dialog('close');
                }
            }]
        });
        $('#dd').dialog('close');

        $.post("/GenericSearch/GetModelColumns?dbName=" + encodeURI($.getUrlParam("dbName")), { id: $("#hddId").val(), t: Date() }, function (result) {
            if (result && result.length > 0) {
                var columnsArray = new Array();
                for (var i = 0; i < result.length; i++) {
                    var columnsTitle = "<a href=\"javascript:void(0)\" title=\"" + result[i].ColumnDescription + "\" style=\"cursor:pointer;text-decoration:none;\" >" + result[i].ColumnName + "</a>"
                    columnsArray.push({ field: result[i].ColumnName, title: columnsTitle, width: 200, align: "center" });
                }
                var dataGridWidth = (result.length * 200);
                var lastIndex;
                $("#whereConditionDataGrid").datagrid({
                    width: 600,
                    height: 270,
                    autoRowHeight: false,
                    fitColumns: true,
                    pagination: false,
                    rownumbers: true,
                    singleSelect: true,
                    url: '/GenericSearch/GetModelColumns?dbName=' + encodeURI($.getUrlParam("dbName")) + '&id=' + $("#hddId").val(),
                    columns: [[
                      { field: 'ColumnName', title: '列名', width: 200 }, 
                      { field: 'ColumnOperator', title: '操作符', width: 100, align: 'center', editor: { type: 'combobox', options: { valueField: 'LegalOperator', textField: 'LegalOperator', url: '/GenericSearch/GetOperatorList?dbName=' + encodeURI($.getUrlParam("dbName")), editable: false}} },
                      { field: 'ColumnValue', title: '值', width: 200, align: 'center', editor: { type: 'text'} }
                    ]],
                    onClickRow: function (rowIndex) {
                        if (lastIndex != rowIndex) {
                            $('#whereConditionDataGrid').datagrid('endEdit', lastIndex);
                            $('#whereConditionDataGrid').datagrid('beginEdit', rowIndex);
                        }
                        lastIndex = rowIndex;
                    }
                });
                var tbdesc = '(' + $("#hddTableDescription").val() + ')';
                if ($("#hddTableDescription").val() == "")
                    tbdesc = "";
                $("#dataGrid").datagrid({
                    width: dataGridWidth,
                    height: 390,
                    autoRowHeight: false,
                    fitColumns: true,
                    pagination: true,
                    rownumbers: true,
                    striped: true,
                    singleSelect: true,
                    idField: "GUID",
                    title: '查询结果' + tbdesc,
                    iconCls: 'icon-save',
                    url: '/GenericSearch/GetViewData?dbName=' + encodeURI($.getUrlParam("dbName")) + '&schemaName=' + encodeURI($("#hddSchemaName").val()) + '&viewName=' + encodeURI($("#hddDefaultView").val()),
                    columns: [columnsArray],
                    toolbar: [{
                        id: 'btnSearch',
                        text: '在结果中查找',
                        iconCls: 'icon-search',
                        handler: function () {
                            $('#dd').dialog('open');
                        }
                    }]
                });
            }
        });
    });

    function SearchData() {
        $('#whereConditionDataGrid').datagrid('acceptChanges');
        var data = $('#whereConditionDataGrid').datagrid("getData").rows;
        var keyWords = "[";
        for (var i = 0; i < data.length; i++) {
            if ($.trim(data[i].ColumnValue) != "") {
                keyWords += "{ColumnName:'" + data[i].ColumnName + "',ColumnOperator:'" + data[i].ColumnOperator + "',ColumnValue:'" + data[i].ColumnValue + "'}";
                keyWords += ",";
            }
        }
        if (keyWords.length > 2)
            keyWords = keyWords.substr(0, keyWords.length - 1);
        keyWords += "]";
        $("#dataGrid").datagrid({
            url: '/GenericSearch/GetViewData/?dbName=' + encodeURI($.getUrlParam("dbName")) + '&schemaName=' + encodeURI($("#hddSchemaName").val()) + '&viewName=' + encodeURI($("#hddDefaultView").val()) + '&keyWords=' + encodeURI(keyWords),
            pageNumber: 1
        });
    }
</script>
<div id="dd" data-options="iconCls:'icon-search'" style="padding: 5px; width: 650px;
    height: 360px;">
    <table id="whereConditionDataGrid">
    </table>
</div>
<table id="dataGrid">
</table>
